#!/bin/sh

## 简单的基于时间序列的预测
## 预测20150620的数据准确性 sh userModel_monitor.sh 20150620
## 根据前15天的数据判断20日的数据是否合理

source ~/.bashrc

## 日期处理
declare -i cur_date
if [[ $# -eq 3 ]];then
	cur_date=$1
	path=$2
	weight=$3
else
	echo "usage:sh userModel_monitor.sh 20150601 path weight"
	exit 1
fi

## 报警短信
phone=(13811063282 13261676918 13520011291 13426082139 13601248681 18610507072)
phone=(13811063282) 
function sendMsg(){
	host=$(hostname)
	for number in $phone
	do
		gsmsend-script $number@$host "$path/$cur_date"
	done
}

## 向上推15天的数据
ago=7
endDate=$(date "+%Y%m%d" -d "-$ago day $cur_date")
## user_login预测

curSize=$(hdfs dfs -du -s $path/dt=$cur_date | cut -d" " -f1)
now_date=$(date "+%Y%m%d" -d "-1 day $cur_date")
declare -i totalSize=0
while [[ $now_date -ge $endDate ]]
do
	size=$(hdfs dfs -du -s $path/dt=$now_date | cut -d" " -f1)
	if [[ $? -ne 0 ]];then
		echo "error exit"
		sendMsg $path/$now_date
		exit 1
	fi
	totalSize=$(bc <<< "$totalSize+$size")
	now_date=$(date "+%Y%m%d" -d "-1 day $now_date")
done
avg=$(bc <<< "scale=3;$totalSize/1024/1024/1024/$ago")
curSize_1=$(bc <<< "scale=3;$curSize/1024/1024/1024")
geAvgSize=$(bc <<< "scale=3;$avg+$weight")
leAvgSize=$(bc <<< "scale=3;$avg-$weight")
c1=$(bc <<< "$curSize_1<=$geAvgSize")
c2=$(bc <<< "$curSize_1>=$leAvgSize")
if [[ $c1 -eq 1 && $c2 -eq 1 ]];then
	echo -n "$cur_date--$curSize_1==$geAvgSize==$leAvgSize=="
	echo "vaild"
else
	echo -n "$cur_date--$curSize_1==$geAvgSize==$leAvgSize=="
	echo "invalid"
	sendMsg $path/$cur_date
fi


userLoginPath=/user/hive/warehouse/pass_data.db/user_login
userRegPath=/user/hive/warehouse/pass_data.db/user_reg

## login的波动范围在3G之间，reg波动在0.5G之间
#calculate $userLoginPath 3
#calculate $userRegPath 0.5
